package com.wmlt.util;


import com.alibaba.fastjson.JSON;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.wmlt.entity.FileResponseData;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Hashtable;
import java.util.List;

/**
 * @author liyuxiang
 * @description: 二维码工具类
 * @param:
 * @return:
 * @date: 2021/4/13 4:52 下午
 */
public class QRCodeUtil {
    private static final String CHARSET = "utf-8";
    // LOGO宽度
    private static final int WIDTH = 60;
    // LOGO高度
    private static final int HEIGHT = 60;

    /**
     * 生成二维码
     *
     * @param content 二维码内容
     * @param size    二维码大小
     * @return
     * @throws Exception
     */
    private static BufferedImage createImage(String content, Integer size) throws Exception {
        Hashtable hints = new Hashtable();
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
        hints.put(EncodeHintType.MARGIN, 1);
        BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, size, size,
                hints);
        int width = bitMatrix.getWidth();
        int height = bitMatrix.getHeight();
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
            }
        }
        return image;
    }

    public static void mkdirs(String destPath) {
        File file = new File(destPath);
        // 当文件夹不存在时，mkdirs会自动创建多层目录，区别于mkdir．(mkdir如果父目录不存在则会抛出异常)
        if (!file.exists() && !file.isDirectory()) {
            file.mkdirs();
        }
    }

    /**
     * 生成二维码图片
     *
     * @param content 地址
     * @param size    长宽
     * @throws Exception
     */
    public static String encode(String content, Integer size) throws Exception {
        BufferedImage image = QRCodeUtil.createImage(content, size);
        String fileUrl = uploadImg(image);
        return fileUrl;
    }


    public static String uploadImg(BufferedImage image) {
        String fileUrl = "";
        try {
            //存放
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            ImageIO.write(image, "jpg", outputStream);
            InputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
            String[] strings = FastdfsUtil.uploadFile(inputStream, "file.jpg");
            String path = "";
            for (String value : strings) {
                path += "/" + value;
            }
            fileUrl = "http://43.128.27.123:12100" + path;
            System.out.println(fileUrl);


        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            fileUrl = "";

        }
        return fileUrl;
    }

}